package com.zhiyou.text.controller;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.text.model.JsModel;
import com.zhiyou.text.model.TextModel;
import com.zhiyou.text.service.TextService;

@Controller
public class TextController {

	@Autowired
	TextService service;

	@RequestMapping(value = "index.do", method = RequestMethod.POST)
	public String index(HttpSession session, @RequestParam("avator") MultipartFile image, Model model) {
		// System.out.println(">>>>><<<<<" + image);
		// TextModel tModel = new TextModel();
		// String name = image.getOriginalFilename();
		// String path = session.getServletContext().getRealPath("") +
		// File.separator + "avatar";
		// File file = new File(path + File.separator + name);
		// if (!file.exists()) {
		// file.mkdirs();
		// }
		// if (!image.isEmpty()) {
		// try {
		// image.transferTo(file);
		// } catch (IllegalStateException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		// if (!image.isEmpty()) {
		// tModel.setAvator("avatar" + File.separator + name);
		// }
		// tModel.setUpdateTime(new Date());
		// boolean b = service.update(tModel);
		// if (b) {
		// model.addAttribute("update", "修改信息成功");
		// return "redirect:info.do";
		// }
		return "info";
	}

	@RequestMapping(value = "js.do", method = RequestMethod.GET)
	public String jString(Model model) {
		List<JsModel> list = service.queryJs();
		model.addAttribute("jsM", list);
		return "js";
	}

	@ResponseBody
	@RequestMapping(value = "js.do", method = RequestMethod.POST)
	public HashMap JString(Model model) {
		HashMap map = new HashMap<>();
		List<JsModel> list = service.queryJs();
		if (list.size() > 0) {
			model.addAttribute("jsM", list);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "query.do", method = RequestMethod.POST)
	public HashMap query(String name, Integer age) {
		HashMap map = new HashMap<>();
		System.out.println(name+","+age);
		if (name != null && age != null) {
			System.out.println(name+","+age);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		return map;
	}

}
